package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import pojo.User;
import util.DBUtil;
import dao.UserDao;

@Component(value="udi")
public class UserDaoImpl implements UserDao {

	@Override
	public int addUser(User user) {
		String sql = "insert into customers(id,name,sal,birth,sex) values (?,?,?,?,?)";
		Connection con =null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, user.getId());
			pst.setString(2, user.getName());
			pst.setDouble(3, user.getSal());
			pst.setDate(4, new java.sql.Date(user.getBirth().getTime()));
			pst.setBoolean(5, user.isSex());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		
		System.out.println(m);
		return m;
	}

	@Override
	public int deleteUser(String id) {
		String sql = "delete from customers where id =?";
		int m = 0;
		PreparedStatement pst = null;
		Connection con = DBUtil.createConnection();
		try {
			pst=con.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		System.out.println(m);
		return m;
	}

	@Override
	public int updateUser(User user) {
		String sql = "update customers set name=? ,sal =?,birth =?,sex =?where id =?";
		PreparedStatement pst = null;
		int m = 0;
		Connection con = null;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, user.getName());
			pst.setDouble(2, user.getSal());
			pst.setDate(3, new java.sql.Date(user.getBirth().getTime()));
			pst.setBoolean(4, user.isSex());
			pst.setString(5, user.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		return m;
	}

	@Override
	public List<User> findAllUsers() {
		String sql = "select id,name,sal,birth,sex from customers";
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		Connection con =null;
		User user;
		try {
		    con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				String name = rs.getString("name");
				String id = rs.getString("id");
				Date birth = new Date(rs.getDate("birth").getTime());
				double sal = rs.getDouble("sal");
				Boolean sex = rs.getBoolean("sex");
				user =new User(id,name,sex,sal,birth);
				list.add(user);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return list;
	}

}
